#pragma once
#include "Manager.h"
#include <iostream>
#include "msclr\marshal_cppstd.h"
#include <string>

//@author A0086933U
namespace CS2103v01 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for AddCategory
	/// </summary>
	public ref class AddCategory : public System::Windows::Forms::Form
	{
	public:
		AddCategory(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			TM = Manager::get();
			label2->Text="";
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~AddCategory()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	protected: 
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::TextBox^  textBox1;

	private:
		/// <summary>
		/// Required designer variable.
		Manager* TM;
	private: System::Windows::Forms::Label^  label2;
			 /// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->button1->Location = System::Drawing::Point(251, 147);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 0;
			this->button1->Text = L"OK";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &AddCategory::button1_Click);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 9, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->label1->Location = System::Drawing::Point(59, 75);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(130, 17);
			this->label1->TabIndex = 1;
			this->label1->Text = L"New Category Name";
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(216, 73);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(100, 21);
			this->textBox1->TabIndex = 2;
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft YaHei", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(134)));
			this->label2->ForeColor = System::Drawing::Color::Red;
			this->label2->Location = System::Drawing::Point(216, 94);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(39, 16);
			this->label2->TabIndex = 3;
			this->label2->Text = L"label2";
			// 
			// AddCategory
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(386, 212);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->button1);
			this->Name = L"AddCategory";
			this->Text = L"Add Category";
			this->Load += gcnew System::EventHandler(this, &AddCategory::AddCategory_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void AddCategory_Load(System::Object^  sender, System::EventArgs^  e) {
				 label2->Text="";
			 }
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 if(!checkField(textBox1, label2))
					 return;
				 string NEW =  msclr::interop::marshal_as<std::string>(textBox1->Text);
				 if(TM->addCategory(NEW))
					 this->Close();
				 else
					 label2->Text="Existed";
			 }
	private: bool checkField(TextBox^ cat, Label^ catL)
			 {
				 catL->Text="";
				 bool miss=false;
				 bool format=true;

				 //check empty field
				 if(cat->Text == "")
				 {
					 catL->Text="missing";
					 miss = true;
				 }
				 if(miss)
					 return false;

				 //check format
				 return format;
			 }
	};
}
